﻿namespace UnityEditor
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    [Serializable]
    internal class FoldoutComponentState
    {
        private bool m_Animated;
        private CurveState[] m_CurveStates;
        private bool m_Expanded;
        private UnityEngine.Object m_Object;

        public FoldoutComponentState(UnityEngine.Object obj, SerializedStringTable expandedFoldouts, Transform tr, int level)
        {
            this.m_Object = obj;
            if ((level == 0) && (this.m_Object.GetType() == typeof(Transform)))
            {
                this.m_Expanded = true;
            }
            else if (expandedFoldouts.Contains(this.GetString(tr)))
            {
                this.m_Expanded = true;
            }
        }

        public void AddChildCurvesToList(List<CurveState> curves, AnimationHierarchyData data)
        {
            if (this.expanded)
            {
                foreach (CurveState state in this.curveStates)
                {
                    if (state.animated || data.animationWindow.showAllProperties)
                    {
                        curves.Add(state);
                    }
                }
            }
        }

        public void Collapse(AnimationHierarchyData data, Transform tr, int level)
        {
            this.m_Expanded = false;
            this.Hide(data, tr, level);
            if ((level > 0) || (this.m_Object.GetType() != typeof(Transform)))
            {
                data.expandedFoldouts.Remove(this.GetString(tr));
            }
            data.animationWindow.SetDirtyPropertyList();
        }

        public void Expand(AnimationHierarchyData data, Transform tr, int level)
        {
            this.m_Expanded = true;
            this.Show(data, tr, level);
            if ((level > 0) || (this.m_Object.GetType() != typeof(Transform)))
            {
                data.expandedFoldouts.Set(this.GetString(tr));
            }
            data.animationWindow.SetDirtyPropertyList();
        }

        public string GetString(Transform tr)
        {
            return (PathUtility.GetPath(tr) + "/:" + ObjectNames.GetInspectorTitle(this.obj));
        }

        private void HandleClick(Rect r, AnimationHierarchyData data)
        {
            Event current = Event.current;
            if ((current.type == EventType.MouseDown) && r.Contains(current.mousePosition))
            {
                bool additive = current.shift || EditorGUI.actionKey;
                List<CurveState> curves = new List<CurveState>();
                this.AddChildCurvesToList(curves, data);
                if (curves.Count > 0)
                {
                    data.animationWindow.SelectProperties(curves[0], curves.Count, additive);
                }
                current.Use();
            }
        }

        public void Hide(AnimationHierarchyData data, Transform tr, int level)
        {
            this.m_CurveStates = null;
        }

        public void OnGUI(AnimationHierarchyData data, string goPath, int level, List<CurveState> shownProperties, Transform tr)
        {
            GUILayoutOption[] options = new GUILayoutOption[] { GUILayout.ExpandWidth(true) };
            int width = (int) GUILayoutUtility.GetRect((float) 10f, (float) 0f, options).width;
            GUIContent content = EditorGUIUtility.TempContent(ObjectNames.GetInspectorTitle(this.obj), AssetPreview.GetMiniTypeThumbnail(this.obj));
            if (Event.current.type == EventType.Repaint)
            {
                data.animationWindow.DrawRowBackground(width, 15);
            }
            Rect objectLabelRect = data.animationWindow.GetObjectLabelRect((float) width);
            this.HandleClick(data.animationWindow.GetObjectLabelRect2((float) width), data);
            bool flag = EditorGUI.Foldout(objectLabelRect, this.expanded, content);
            if (flag != this.expanded)
            {
                if (flag)
                {
                    this.Expand(data, tr, level);
                }
                else
                {
                    this.Collapse(data, tr, level);
                }
            }
            if (this.animated)
            {
                data.animationSelection.DrawRightIcon(CurveUtility.GetIconKey(), (Color) AnimationWindow.kAnimatedColor, (float) width);
            }
            AnimationWindow.vPosition += 15;
            if (this.expanded)
            {
                foreach (CurveState state in this.curveStates)
                {
                    if (data.showAllProperties || state.animated)
                    {
                        if (goPath != state.curveData.path)
                        {
                            Debug.LogError("goPath!=CurveData.path: " + goPath + " vs " + state.curveData.path);
                        }
                        state.guiPosition = data.animationWindow.GetPropertyPos((float) width);
                        state.even = AnimationWindow.kEvenRow;
                        AnimationWindow.kEvenRow = !AnimationWindow.kEvenRow;
                        shownProperties.Add(state);
                        AnimationWindow.vPosition += 12;
                    }
                }
            }
        }

        public void RefreshAnimatedState(AnimationHierarchyData data, Transform tr)
        {
            this.m_Animated = false;
            string path = AnimationUtility.CalculateTransformPath(tr, data.animated.transform);
            IEnumerator enumerator = data.animatedPaths.Keys.GetEnumerator();
            try
            {
                while (enumerator.MoveNext())
                {
                    int current = (int) enumerator.Current;
                    if (current == CurveUtility.GetPathAndTypeID(path, this.obj.GetType()))
                    {
                        this.m_Animated = true;
                        return;
                    }
                }
            }
            finally
            {
                IDisposable disposable = enumerator as IDisposable;
                if (disposable == null)
                {
                }
                disposable.Dispose();
            }
        }

        public void Show(AnimationHierarchyData data, Transform tr, int level)
        {
            if (!this.expanded)
            {
                this.RefreshAnimatedState(data, tr);
            }
            else
            {
                string str = AnimationUtility.CalculateTransformPath(tr, data.animated.transform);
                AnimationClipCurveData[] animatablePropertiesForObject = AnimationUtility.GetAnimatablePropertiesForObject(this.obj, tr.gameObject);
                foreach (AnimationClipCurveData data2 in animatablePropertiesForObject)
                {
                    data2.path = str;
                }
                this.m_Animated = false;
                if (this.obj.GetType() == typeof(Transform))
                {
                    this.m_CurveStates = new CurveState[9];
                    this.m_CurveStates[0] = new CurveState(animatablePropertiesForObject[4]);
                    this.m_CurveStates[1] = new CurveState(animatablePropertiesForObject[5]);
                    this.m_CurveStates[2] = new CurveState(animatablePropertiesForObject[6]);
                    this.m_CurveStates[3] = new CurveState(new AnimationClipCurveData());
                    this.m_CurveStates[4] = new CurveState(new AnimationClipCurveData());
                    this.m_CurveStates[5] = new CurveState(new AnimationClipCurveData());
                    for (int i = 0; i < 3; i++)
                    {
                        this.m_CurveStates[i + 3].curveData.path = animatablePropertiesForObject[i].path;
                        this.m_CurveStates[i + 3].curveData.propertyName = animatablePropertiesForObject[i].propertyName;
                        this.m_CurveStates[i + 3].curveData.target = animatablePropertiesForObject[i].target;
                        this.m_CurveStates[i + 3].curveData.type = animatablePropertiesForObject[i].type;
                    }
                    this.m_CurveStates[6] = new CurveState(animatablePropertiesForObject[7]);
                    this.m_CurveStates[7] = new CurveState(animatablePropertiesForObject[8]);
                    this.m_CurveStates[8] = new CurveState(animatablePropertiesForObject[9]);
                    for (int j = 0; j < this.m_CurveStates.Length; j++)
                    {
                        int curveID = CurveUtility.GetCurveID(data.clip, this.m_CurveStates[j].curveData);
                        if (data.animatedCurves.ContainsKey(curveID))
                        {
                            this.m_Animated = true;
                            this.m_CurveStates[j].animated = true;
                        }
                    }
                    AnimationCurve curve = null;
                    if (data.clip != null)
                    {
                        curve = AnimationUtility.GetEditorCurve(data.clip, this.m_CurveStates[3].curveData.path, this.m_CurveStates[3].curveData.type, "localEulerAngles.x");
                    }
                    if (curve == null)
                    {
                        this.m_CurveStates[3].curveData.propertyName = "localEulerAnglesBaked.x";
                        this.m_CurveStates[4].curveData.propertyName = "localEulerAnglesBaked.y";
                        this.m_CurveStates[5].curveData.propertyName = "localEulerAnglesBaked.z";
                    }
                    else
                    {
                        this.m_CurveStates[3].curveData.propertyName = "localEulerAngles.x";
                        this.m_CurveStates[4].curveData.propertyName = "localEulerAngles.y";
                        this.m_CurveStates[5].curveData.propertyName = "localEulerAngles.z";
                    }
                }
                else
                {
                    this.m_CurveStates = new CurveState[animatablePropertiesForObject.Length];
                    for (int k = 0; k < this.m_CurveStates.Length; k++)
                    {
                        this.m_CurveStates[k] = new CurveState(animatablePropertiesForObject[k]);
                        int key = CurveUtility.GetCurveID(data.clip, this.m_CurveStates[k].curveData);
                        if (data.animatedCurves.ContainsKey(key))
                        {
                            this.m_Animated = true;
                            this.m_CurveStates[k].animated = true;
                        }
                    }
                }
                foreach (CurveState state in this.m_CurveStates)
                {
                    state.color = CurveUtility.GetPropertyColor(state.curveData.propertyName);
                    state.animationSelection = data.animationSelection;
                }
            }
        }

        public bool animated
        {
            get
            {
                return this.m_Animated;
            }
        }

        public CurveState[] curveStates
        {
            get
            {
                return this.m_CurveStates;
            }
        }

        public bool expanded
        {
            get
            {
                return this.m_Expanded;
            }
        }

        public UnityEngine.Object obj
        {
            get
            {
                return this.m_Object;
            }
            set
            {
                this.m_Object = value;
            }
        }
    }
}

